package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.service.ApiT_tokenService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "token/")
public class ApiTokenController {

	@Autowired
	private CheckService checkService;
	@Autowired
	private ApiT_tokenService apiT_tokenService;

	private static Logger logger = Logger.getLogger(ApiTokenController.class);

	/**
	 * 配送端实时更新地址 经纬度
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "updateSendAddress")
	public void updateSendAddress(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String u_id = request.getParameter("u_id");
			String token = request.getParameter("token");
			String longitude = request.getParameter("longitude");// 经度
			String latitude = request.getParameter("latitude");// 经度
			if (checkService.checkToken(u_id, token, map)) {
				Map<String, String> params = new HashMap<String, String>();
				params.put("u_id", u_id);
				params.put("token", token);
				params.put("longitude", longitude);
				params.put("latitude", latitude);
				apiT_tokenService.updateSendAddress(params, map);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e);
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(JsonUtil.object2json(map));
		out.close();
	}
}
